import {v3} from "cc";
import {Stage} from "db://assets/scripts/data/model/Stage";
import {StageField} from "db://assets/scripts/data/db/StageField";
import {CSV} from "db://assets/scripts/common/CSV";


/**
 * 关卡数据转换器
 * public convertFromVector: (V) -> T
 * public convertToVector: (T) -> V
 */
export class StageConverter {
    /**
     * 禁止创建对象
     */
    private constructor() {
    }

    /**
     * 转数组
     *
     * @param csv CSV
     */
    public static convertFromCSV(csv: CSV): Stage[] {
        let result: Stage[] = [];
        csv.datas.forEach((row) => {
            result.push(this.convertFromRow(row));
        });
        return result;
    }

    /**
     * 转对象
     *
     * @param row CSV行数据
     */
    public static convertFromRow(row: string[]): Stage {
        let positions = row[StageField.POSITION].split(",").map(value => Number(value));
        let angles = row[StageField.ANGLE].split(",").map(value => Number(value));
        let scales = row[StageField.SCALE].split(",").map(value => Number(value));
        return {
            id: row[StageField.ID],
            position: v3(positions[0], positions[1], positions[2]),
            angle: v3(angles[0], angles[1], angles[2]),
            scale: v3(scales[0], scales[1], scales[2]),
            skill: row[StageField.SKILL],
            movePattern: Number(row[StageField.MOVE_PATTERN]),
        };
    }
}